package com.ywjt.platformbusiness.store.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.store.adapter.GoodsTypeSelectAdapter;
import com.ywjt.platformbusiness.store.model.GoodsClassOneModel;
import com.ywjt.platformbusiness.util.JsonUtils;
import com.ywjt.platformbusiness.widget.MyLayoutManager;

import org.json.JSONException;
import org.json.JSONObject;

public class ActivityGoodsType extends BaseActivity implements View.OnClickListener {
    private RecyclerView rcCommon;
    private GoodsTypeSelectAdapter typeAdapter;
    private TextView tvSure;
    private GoodsClassOneModel model;

    private String typeid="";
    private String type="";


    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityGoodsType.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_goods_type;
    }

    @Override
    protected void initView() {
        setTitleText("选择商品分类");
        rcCommon=findViewById(R.id.rcCommon);
        tvSure=findViewById(R.id.tvSure);

        tvSure.setOnClickListener(this::onClick);
        getTypeData();
    }

    @Override
    protected void initData() { }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }


    private void getTypeData() {
        new HttpRequest(getContext()).doGet(UrlConstants.classify_getShopSubordinateClassify, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), GoodsClassOneModel.class);
                        if (obj instanceof GoodsClassOneModel) {
                             model = (GoodsClassOneModel) obj;
                            if (model.getResult().size()>0) {

                                MyLayoutManager myLayoutManager = new MyLayoutManager(ActivityGoodsType.this, true);
                                 typeAdapter = new GoodsTypeSelectAdapter(model.getResult());
                                rcCommon.setLayoutManager(myLayoutManager);
                                rcCommon.setAdapter(typeAdapter);
                                typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                            for (int i=0;i<model.getResult().size();i++){
                                                model.getResult().get(i).setSelected(false);
                                            }
                                        typeid=model.getResult().get(position).getId();
                                            type=model.getResult().get(position).getName();
                                            model.getResult().get(position).setSelected(true);
                                            typeAdapter.notifyDataSetChanged();
                                    }
                                });
                            }
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * Called when a view has been clicked.
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSure:
                //确定
                if (typeid.equals("")||type.equals("")){
                    Toast.makeText(ActivityGoodsType.this,"请先选择商品分类",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    Intent intent = new Intent();
                    intent.putExtra("id",typeid);
                    intent.putExtra("name",type);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
        }
    }
}